﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) www.51mvc.com Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.51mvc.com/
 * Create By 小鱼(Leven)  - http://leven.com.cn
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using Infancy.Core.Entities;
using Infancy.Core.Repository;

namespace Infancy.Data.Aef {
    public class FriendLinkRepository : AefRepositoryBase<FriendLink, FriendLink>, IFriendLinkRepository {
        public FriendLinkRepository(RepositoryContext context)
            : base(context) {
        }

        public override FriendLink Create() {
            return new FriendLink();
        }

        #region Test GUID methods
        private static IList<FriendLink> database = new List<FriendLink>();
        private static object lockObject = new object();

        public override void Insert(Infancy.Core.Entities.FriendLink entity) {
            lock (lockObject) {
                FriendLink maxItem = database.OrderByDescending(f => f.ID).FirstOrDefault();
                if (maxItem == null)
                    entity.ID = 1;
                else
                    entity.ID = maxItem.ID + 1;
                database.Add(entity);
            }
        }

        public override void Update(FriendLink entity) {
            FriendLink find = GetSingle(entity.ID);
            if (find != null) {
                lock (lockObject) {
                    find = entity;
                }
            }
        }

        public override void Delete(FriendLink entity) {
            Delete(entity.ID);
        }

        public override void Delete(long id) {
            FriendLink find = GetSingle(id);
            if (find != null) {
                lock (lockObject) {
                    database.Remove(find);
                }
            }
        }

        public override int Count() {
            return database.Count;
        }

        public override FriendLink GetSingle(long id) {
            FriendLink find = null;
            foreach (FriendLink item in database) {
                if (item.ID == id) {
                    find = item;
                    break;
                }
            }
            return find;
        }

        public override IList<FriendLink> GetList() {
            return database;
        }

        #endregion
    }
}
